﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Models.Home
{
    public partial  class RssModel
    {
        public int ArticleId { get; set; }
        public int UserId { get; set; }
        public int ContentSectionId { get; set; }

    }
    public partial class RssModel
    {
        public IUser Author
        {
            get
            {
                if (_author == null)
                {
                    _author = new Lazy<IUser>(() => _userQueries.TryGetWhereId(this.UserId));
                }
                return _author.Value;
            }
        }
        private Lazy<IUser> _author;

        public List<IArticle> ArticlesForAuthor
        {
            get
            {
                if (_articlesForAuthor == null)
                {
                    _articlesForAuthor = new Lazy<List<IArticle>>(() => _articleQueries.GetArticlesWrittenByAuthor(this.UserId,false).ToList());
                }
                return _articlesForAuthor.Value;
            }
        }
        private Lazy<List<IArticle>> _articlesForAuthor;

        public IArticle Article
        {
            get
            {
                if (_article == null)
                {
                    _article = new Lazy<IArticle>(() => _articleQueries.GetArticle(this.ArticleId));
                }
                return _article.Value;
            }
        }
        private Lazy<IArticle> _article;
       
        public List<IArticle> LatestArticles
        {
            get
            {
                if (_latestArticles == null)
                {
                    _latestArticles = new Lazy<List<IArticle>>(() => _articleQueries.GetLatestArticles(Convert.ToInt32(ConfigurationManager.AppSettings["RssLatestArticleIndexCount"])).ToList());
                }
                return _latestArticles.Value;
            }
        }
        private Lazy<List<IArticle>> _latestArticles;
        
        public List<ISubSection> SubSections
        {
            get
            {
                if (_subSections == null)
                {
                    _subSections = new Lazy<List<ISubSection>>(() => _subSectionQueries.GetByName("Izdvojeno").ToList());
                }
                return _subSections.Value;
            }
        }
        private Lazy<List<ISubSection>> _subSections;

        public List<Tuple<ISubSection,List<IArticle>>> ArticlesForSubSections
        {
            get
            {
                if (_articlesForSubections == null)
                {
                    _articlesForSubections = new Lazy<List<Tuple<ISubSection,List<IArticle>>>>(() => 
                        (from s in SubSections
                         let articles=_articleQueries.GetArticlesForSubSection(s.Id).ToList()
                         select new Tuple<ISubSection,List<IArticle>>(s,articles)).ToList()) ;
                }
                return _articlesForSubections.Value;
            }
        }
        private Lazy<List<Tuple<ISubSection, List<IArticle>>>> _articlesForSubections;

        public List<IArticle> ArticlesForContentSection
        {
            get
            {
                _articlesForContentSection = new Lazy<List<IArticle>>(() => _articleQueries.GetArticlesForContentSection(ContentSectionId).ToList());
                
                return _articlesForContentSection.Value;
            }
        }
        private Lazy<List<IArticle>> _articlesForContentSection;
    }
    public partial class RssModel
    {
        private readonly IArticleQueries _articleQueries;
        private readonly ISubSectionQueries _subSectionQueries;
        private readonly IUserQueries _userQueries;
        public RssModel(IArticleQueries articleQueries,
            ISubSectionQueries subSectionQueries,
            IUserQueries userQueries)
        {
            _articleQueries = articleQueries;
            _subSectionQueries = subSectionQueries;
            _userQueries = userQueries;

        }
    }
}